﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using Telerik.WinControls.UI;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class ChonTaiLieu : Form
    {
        private PhieuBanGiaoTaiLieuChiTiet result;
        public PhieuBanGiaoTaiLieuChiTiet Result
        {
            get
            {
                return result;
            }
        }       

        public ChonTaiLieu()
        {
            InitializeComponent();

            ddlLoaiBieuPhi.Items.Clear();
            ddlLoaiBieuPhi.Items.Add("Tham khảo tài liệu");
            ddlLoaiBieuPhi.Items.Add("Khai thác và sử dụng tài liệu");

            labelMucThu.Visible = false;
            drlMucThu.Visible = false;
            drlMucThu.TabStop = false;

            button1.DialogResult = System.Windows.Forms.DialogResult.OK;
            button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void Close(object sender, EventArgs e)
        {

        }

        private void Select(object sender, EventArgs e)
        {
            if (Validate())
            {
                result = new PhieuBanGiaoTaiLieuChiTiet();
                result.TenTaiLieu = ddlTenLoaiTaiLieu.SelectedItem.Text;
                result.MaTaiLieu = textMaTaiLieu.Text;
                result.DonGia = Convert.ToDecimal(numberDonGia.Value);
                result.DonViTinh = textDonViTinh.Text;
                this.Close();
            }
        }

        private bool Validate()
        {
            var hasError = true;
            if (string.IsNullOrEmpty(ddlTenLoaiTaiLieu.SelectedItem.Text))
            {
                errorProvider.SetError(ddlTenLoaiTaiLieu, Constants.Messages.RequireMessage);
                hasError = false;                
            }

            if (string.Equals(ddlLoaiBieuPhi.SelectedItem.Text, "Khai thác và sử dụng tài liệu", StringComparison.InvariantCultureIgnoreCase) &&
                string.IsNullOrEmpty(drlMucThu.SelectedItem.Text))
            {
                errorProvider.SetError(drlMucThu, Constants.Messages.RequireMessage);
                hasError = false;
            }

            return hasError;
        }

        private void SelectBieuPhi(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            ddlLoaiTaiLieu.Items.Clear();
            ddlDangLoaiTaiLieu.Items.Clear();
            ddlTenLoaiTaiLieu.Items.Clear();
            textMaTaiLieu.Text = string.Empty;            

            var selected = ddlLoaiBieuPhi.SelectedItem.Text;

            if (string.Equals(selected, "Tham khảo tài liệu", StringComparison.InvariantCultureIgnoreCase))
            {
                panelDetail.Top = panelLoaiTaiLieu.Top;

                panelLoaiTaiLieu.Visible = false;
                ddlLoaiTaiLieu.TabStop = false;
                ddlDangLoaiTaiLieu.TabStop = false;

                labelMucThu.Visible = false;
                drlMucThu.Visible = false;
                drlMucThu.TabStop = false;

                LoadTaiLieuThamKhao();
            }
            else
            {
                panelDetail.Top = 137;
                panelLoaiTaiLieu.Visible = true;
                ddlLoaiTaiLieu.TabIndex = 1;
                ddlDangLoaiTaiLieu.TabIndex = 2;

                drlMucThu.Items.Clear();
                labelMucThu.Visible = true;
                drlMucThu.Visible = true;
                drlMucThu.TabStop = true;

                LoadLoaiTaiLieuKhaiThac();
            }
        }

        private void SelectLoaiTaiLieu(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            ddlDangLoaiTaiLieu.Items.Clear();
            ddlTenLoaiTaiLieu.Items.Clear();
            textMaTaiLieu.Text = string.Empty;

            LoadDangTaiLieuKhaiThac();
        }

        private void SelectDangTaiLieu(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            ddlTenLoaiTaiLieu.Items.Clear();
            textMaTaiLieu.Text = string.Empty;
            
            string level1 = Convert.ToString(ddlLoaiTaiLieu.SelectedItem);
            string level2 = Convert.ToString(ddlDangLoaiTaiLieu.SelectedItem);
            LoadTaiLieuKhaiThac(level1, level2);
        }

        private void SelectTaiLieu(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            textMaTaiLieu.Text = string.Empty;
            
            var selectedItem = (RadListDataItem)ddlTenLoaiTaiLieu.SelectedItem;
            if (selectedItem == null) return;

            int id = (int)selectedItem.Value;

            var selected = ddlLoaiBieuPhi.SelectedItem.Text;
            if (string.Equals(selected, "Tham khảo tài liệu", StringComparison.InvariantCultureIgnoreCase))
            {
                var biz = new ThamKhaoTaiLieuBiz();
                var item = biz.LoadItem(id);

                textMaTaiLieu.Text = item.MaTaiLieu;

                numberDonGia.Value = item.Phi.HasValue ? item.Phi.Value : 0;
                textDonViTinh.Text = item.DonViTinh;
            }
            else
            {
                var biz = new KhaiThacVaSuDungTaiLieuBiz();
                var item = biz.LoadItem(id);                

                textMaTaiLieu.Text = item.MaTaiLieu;
                AddMucThu("Photo", item.MucThuPhoto, drlMucThu);
                AddMucThu("In", item.MucThuIn, drlMucThu);
                AddMucThu("Word, Excel", item.MucThuOffice, drlMucThu);
                AddMucThu("Rastor", item.MucThuRastor, drlMucThu);
                AddMucThu("Vector", item.MucThuVector, drlMucThu);

                textDonViTinh.Text = item.DonViTinh;
            }
        }

        private void LoadTaiLieuThamKhao()
        {
            var biz = new ThamKhaoTaiLieuBiz();

            var items = biz.ViewAll();
            foreach (ThamKhaoTaiLieu item in items)
            {
                var newItem = new RadListDataItem();
                newItem.Text = item.LoaiTaiLieu;
                newItem.Value = item.Id;
                ddlTenLoaiTaiLieu.Items.Add(newItem);
            }
        }

        private void LoadTaiLieuKhaiThac(string loaiTaiLieu, string dangTaiLieu)
        {
            var biz = new KhaiThacVaSuDungTaiLieuBiz();
            List<KhaiThacVaSuDungTaiLieu> items = null;

            if (string.IsNullOrEmpty(dangTaiLieu))
            {
                items = biz.GetItemByLevel1(loaiTaiLieu);
            }
            else
            {
                items = biz.GetItemByLevel2(loaiTaiLieu, dangTaiLieu);
            }

            foreach (KhaiThacVaSuDungTaiLieu item in items)
            {
                var newItem = new RadListDataItem();
                newItem.Text = item.TenLoaiTaiLieu;
                newItem.Value = item.Id;
                ddlTenLoaiTaiLieu.Items.Add(newItem);
            }
        }

        private void LoadLoaiTaiLieuKhaiThac()
        {
            var biz = new KhaiThacVaSuDungTaiLieuBiz();
            var items = biz.GetAllLevel1();

            ddlLoaiTaiLieu.Items.Clear();

            foreach (string item in items)
            {
                ddlLoaiTaiLieu.Items.Add(item);
            }
        }

        public void LoadDangTaiLieuKhaiThac()
        {
            string level1 = Convert.ToString(ddlLoaiTaiLieu.SelectedItem);

            var biz = new KhaiThacVaSuDungTaiLieuBiz();
            var items = biz.GetAllLevel2ByLevel1(level1);

            ddlDangLoaiTaiLieu.Items.Clear();

            foreach (string item in items)
            {
                ddlDangLoaiTaiLieu.Items.Add(item);
            }
        }

        private void AddMucThu(string name, decimal? gia, RadDropDownList control)
        {
            if (gia.HasValue)
            {
                var item = new RadListDataItem();
                item.Text = name;
                item.Value = gia;
                control.Items.Add(item);
            }
        }

        private void SelectMucThu(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            string value = Convert.ToString(drlMucThu.SelectedItem.Value);
            if (string.IsNullOrEmpty(value))
            {
                numberDonGia.Value = 0;
            }
            else
            {
                numberDonGia.Value = Convert.ToDecimal(value);
            }
        }
    }
}
